/*
 * Copyright 2012 Chris Uyehara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.chrisuyehara.vista.rpc.procedures.XWB;

import com.chrisuyehara.vista.rpc.client.RPCString;

import com.chrisuyehara.vista.rpc.procedures.RawRemoteProcedure;


/**
 * Class description
 *
 *
 * @version        v1.0, 2014-01-20
 * @author         Jim Horner
 */
public class Connect extends RawRemoteProcedure {

    /**
     * Constructs ...
     *
     *
     * @param ipAddress
     * @param hostName
     */
    public Connect(String ipAddress, String hostName) {

        super(createPayload(ipAddress, hostName));
    }

    /**
     * Method description
     *
     *
     * @param ipAddress
     * @param hostName
     *
     * @return
     */
    private static String createPayload(String ipAddress, String hostName) {

        StringBuilder sb = new StringBuilder();

        sb.append("[XWB]10304\nTCPConnect50");
        sb.append(RPCString.packString(ipAddress, 3));
        sb.append("f0");
        sb.append(RPCString.packString("0", 3));
        sb.append("f0");
        sb.append(RPCString.packString(hostName, 3));
        sb.append("f");
        sb.append((char) 4);

        return sb.toString();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public boolean isConnectionAccepted() {
        return "accept".equalsIgnoreCase(getResponse());
    }
}
